package com.du.web.sample.util.state;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc:
 * @author: shuangdu
 * @date: 2022/11/30
 * @since: 1.8
 */
public abstract class AbstractFlow {

    private Map<State, FlowVO> map = new HashMap<>();

    public AbstractFlow() {
    }

    protected final void pushMap(List<FlowVO> flowList) {
        for (FlowVO vo : flowList) {
            map.put(vo.getFrom(), vo);
        }
    }

    public <T> State next(State current, Action action, T param) {
        FlowVO vo = map.get(current);
        if (vo != null) {
            switch (action) {
                case PASS:
                    vo.getHandler().pass(param);
                    break;
                case REJECT:
                    vo.getHandler().reject(param);
                    break;
                default:
                    System.out.println("暂不支持改操作");
                    break;
            }
            return vo.getTo();
        }
        return null;
    }

}
